<?php

namespace App\Http\Controllers\Wap;

use App\Models\News;
use App\Models\Projects;
use App\Models\Seos;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProjectController extends Controller
{
    protected $chanel_id = 2;//设置导航选中标志

    public function __construct()
    {
        view()->share('chanel_id',$this->chanel_id);
        view()->share('seo',Seos::seo('SEO-PROJECT'));
    }

    public function index(Request $request)
    {
        $params = [
            'title'  => (string)$request->get('title',''),
        ];
        $fields = ['id','thumb','title','cateid','region','explain','online'];
        $lists = Projects::list($fields,$params,10);
        if($request->ajax()){
            return $this->writeJson(0,$lists);
        }
        $lists = json_encode($lists);
        $pageTitle = '项目';
        return view('wap.project.index',compact('lists','pageTitle'));
    }


    public function info(Request $request)
    {
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Projects::select([ 'id','thumb','title','cateid','status','region','explain','online','summary',
            'home_link','white_link','github_link','chrome_link','introduce','member','merit','scene','partner',
            'platform','about_project','about_new','view','created_at'])
            ->where('status',Projects::STATUS_1)
            ->with(['cate' => function ($query) {
                $query->select(['id','name']);
            }])->findOrFail($id);
        //相关新闻
        $news = News::select(['id','thumb','author_id','cateid','title','created_at','view','cateid','summary','praise'])
            ->whereIn('id',$info['about_new'])
            ->where('status',News::STATUS_1)
            ->with(['cateChild' => function ($query) {
                $query->select(['id','name'])->get();
            }]) ->with(['user' => function ($query) {
                $query->select(['id','truename','avatar'])->get();
            }])->get();
        foreach($news as &$new){
            $new['created_at'] = $new['created_at']->format('Y-m-d');
        }
        // 统计浏览量
        $info->increment('view');
        return view('wap.project.info',compact('info','news'));
    }

}